package model;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

public class FrameworkModel {
	Document doc;
	public FrameworkModel(Document doc) {
		this.doc = doc;
	}
	
	private void treewalk(FrameworkNode fn, Element element) {
		  for (int i = 0, size = element.nodeCount(); i < size; i++)     {
	           Node node = element.node(i);
	           if (node instanceof Element) {
	        	   FrameworkNode nextGeneration = new FrameworkNode(getTreeString((Element) node));
	        	   fn.addChild(nextGeneration);
	        	   treewalk(nextGeneration, (Element) node);
	           } else { // do something
//	        	   System.out.println("kkkkkkk");
	           }
	       }
	}

	//get the display string of treeItem
    private static String[] getTreeString(Element node) {
	  String attributeString = "";
	  if(node.attributeCount()>0)
		  attributeString = ((Element) node).attribute(0).getName()+"="+((Element) node).attribute(0).getText();
		return new String[]{node.getName(), attributeString};
	}
    public FrameworkNode getModel() {
    	Element rootElementDOM = doc.getRootElement();
    	FrameworkNode fn = null;
		if(rootElementDOM!=null) {
			fn = new FrameworkNode(getTreeString(rootElementDOM));
			treewalk(fn, rootElementDOM);
		}
		return fn;
    }
    
}
